﻿using System;
using System.Security.Principal;
using Castle.ActiveRecord;

namespace Hadlow.Jobvertizer.Models
{
    [ActiveRecord("`User`")]
    public class User : Entity, IPrincipal
    {
        string name;

        [Property]
        public string Name
        {
            get { return name; }
            set 
            { 
                name = value;
                identity = new GenericIdentity(name);
            }
        }

        string password;

        [Property]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        Role role;

        [BelongsTo("RoleId")]
        public Role Role
        {
            get { return role; }
            set { role = value; }
        }

        public static User Guest
        {
            get
            {
                return new User() { Id = 0, Name = "Guest", Role = Role.Guest };
            }
        }

        public override string ToString()
        {
            return name;
        }

        #region IPrincipal Members

        IIdentity identity;

        public IIdentity Identity
        {
            get 
            {
                return identity;
            }
        }

        public bool IsInRole(string role)
        {
            return this.role.Name == role;
        }

        #endregion
    }
}
